import { ArgumentsHost, Catch, ExceptionFilter } from '@nestjs/common';
import { TypeORMError, QueryFailedError } from 'typeorm';

@Catch(TypeORMError)
export class TypeormExceptionFilter implements ExceptionFilter {
    catch(exception: TypeORMError, host: ArgumentsHost) {
        const ctx = host.switchToHttp();
        let code = 500;
        if (exception instanceof QueryFailedError) {
            code = exception.driverError.errno;
        }
        // 响应 请求对象
        const response = ctx.getResponse();
        response.status(500).send({
            code: code,
            timestamp: new Date().toISOString(),
            // path: request.url,
            // method: request.method,
            message: exception.message,
        });
    }
}
